package com.fitnessapp.user;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.fitnessapp.database.DatabaseHelper;
import com.fitnessapp.database.DatabaseStrings;
import com.fitnessapp.database.MySqlHelper;

public class Routine {
	
	private ArrayList<List<UserExercise>> exercies = new ArrayList<List<UserExercise>>(DaysOfTheWeek.NUM_DAYS_IN_WEEK);
	private String name;
	private int id;
	private String authorName;
	private String dateCreated;
	private String goal;
	private boolean publicRoutine;
	
	public Routine(String name, String goal){
		this.name = name;
		this.goal = goal;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		this.dateCreated = format.format(new Date());
		this.authorName = DatabaseHelper.LOCAL_USER;
		
		for(int i = 0; i < DaysOfTheWeek.NUM_DAYS_IN_WEEK; i++){
			exercies.add(new ArrayList<UserExercise>());			
		}
	}
	
	public Routine(){
		for(int i = 0; i < DaysOfTheWeek.NUM_DAYS_IN_WEEK; i++){
			exercies.add(new ArrayList<UserExercise>());			
		}
	}
	
	public void loadFromCursor(Cursor cursor){
		
		this.id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_ID));
		this.name =cursor.getString(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_NAME));
		this.dateCreated = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_CREATION_DATE));
		this.publicRoutine = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_SHARED_FLAG)) != 0;
		this.authorName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_AUTHOR_NAME));
		this.goal = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ROUTINE_TABLE_GOAL));
	
	}

	private void loadExercises(MySqlHelper dbHelper){
//		List<Map<String,String>> result = dbHelper.query(
//				"exercise_id, day_of_week", DatabaseStrings.ROUTINE_EXERCISE_TABLE, "routine_id = \'" + id + "\'");
//		
//		if( result.size() > 0 ){
//			for(Map<String,String> exerciseData : result){
//				int dayOfWeek = Integer.parseInt(exerciseData.get("day_of_week"));
//				
//				Exercise exercise = new Exercise(exerciseData.get("exercise_id"));
//				exercise.load(dbHelper);
//				
//				exercies.get(dayOfWeek).add(exercise);
//				
//			}
//			
//		}
	}
	
	public List<UserExercise> getExercies(int dayOfWeek) { return exercies.get(dayOfWeek); }

	public String getName() { return name; }

	public void loadExercises(SQLiteDatabase db) {
		String query = "SELECT * FROM " + DatabaseHelper.ROUTINE_EXERCISE_TABLE + " re INNER JOIN " + DatabaseHelper.EXERCISE_TABLE + 
				" e ON " + DatabaseHelper.EXERCISE_TABLE_ID + " WHERE re." + DatabaseHelper.ROUTINE_TABLE_ID + "=?";
		
		Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(id)});
		
		if( cursor.moveToFirst() ){
			do{
				UserExercise exercise = new UserExercise();
				exercise.createFromCursor(cursor);				
				exercies.get(exercise.getDayOfWeek()).add(exercise);				
			}while(cursor.moveToNext());
		}		
	}

	public int getId() { return id; }

	public void save(SQLiteDatabase db) {
		String query = "INSERT OR REPLACE INTO " + DatabaseHelper.ROUTINE_TABLE +
				" (" + DatabaseHelper.ROUTINE_TABLE_NAME + ", " +
				DatabaseHelper.ROUTINE_TABLE_GOAL + ", " +
				DatabaseHelper.ROUTINE_TABLE_CREATION_DATE + ", " +
				DatabaseHelper.ROUTINE_TABLE_SHARED_FLAG + ", " +
				DatabaseHelper.ROUTINE_TABLE_AUTHOR_NAME +
				") VALUES (?, ?, ?, ?, ?)";
		
		String [] values = new String[]{this.name, this.goal, this.dateCreated,  (this.publicRoutine ? "1" : "0"), this.authorName};
		
		Log.d("Routine.java", query);
				
		db.rawQuery(query, values);
	}

	public void deleteFromDB(SQLiteDatabase db) {
		String where = DatabaseHelper.ROUTINE_TABLE_NAME + "=?";
		db.delete(DatabaseHelper.ROUTINE_TABLE, where, new String[]{this.name});
	}

}
